package hu.vpszd.services.impl

import hu.vpszd.entities.transaction.LoanApplication
import hu.vpszd.services.MinimumConditionCheckingService

import org.apache.log4j.Logger
import org.springframework.stereotype.Service;

@Service
class MinimumConditionCheckerServiceImpl implements MinimumConditionCheckingService {
	private static final Logger logger = Logger.getLogger(MinimumConditionCheckerServiceImpl.class)
	Boolean isAgeAcceptable
	Boolean isIncomeAcceptable
	Boolean isLenghtOfEmploymentAcceptable = true

	public LoanApplication checkMinimumCondition(LoanApplication loanApplication) {

		Date today = new Date()
		if((today.minus((loanApplication.birthday))/365) > 20){
			isAgeAcceptable = true
		}



		int employeeIncome = 0
		int businessIncome = 0
		int otherIncome = 0
		for(i in loanApplication.incomes){
			if(i.incomeType == "Alkalmazotti"){
				if(i.incomeAmount >= 83000 && ((today.minus(i.lengthOfEmployement)/365) < 1)){
					isLenghtOfEmploymentAcceptable = false
				}
				else if(i.incomeAmount < 83000 && ((today.minus(i.lengthOfEmployement)/365) >= 1)){
					employeeIncome += i.incomeAmount
					isLenghtOfEmploymentAcceptable = false
				}
				else{
					employeeIncome += i.incomeAmount
					isLenghtOfEmploymentAcceptable = true
					break
				}
			}
			else if (i.incomeType == "Vállalkozói"){
				//				businessIncome += i.incomeAmount
				if(i.incomeAmount >= 83000 && ((today.minus(i.lengthOfEmployement)/365) < 1)){
					isLenghtOfEmploymentAcceptable = false
				}
				else if(i.incomeAmount < 83000 && ((today.minus(i.lengthOfEmployement)/365) >= 1)){
					businessIncome += i.incomeAmount
					isLenghtOfEmploymentAcceptable = false
				}
				else{
					businessIncome += i.incomeAmount
					isLenghtOfEmploymentAcceptable = true
					break
				}
			}
			else{
				//TODO
				//otherIncome += i.incomeAmount
			}
		}




		if((employeeIncome >= 83000)){
			isIncomeAcceptable = true
			isLenghtOfEmploymentAcceptable = true
		}
		else if(businessIncome >= 83000){
			isIncomeAcceptable = true
			isLenghtOfEmploymentAcceptable = true
		}
		else if(otherIncome >= 90000){
			isIncomeAcceptable = true
			isLenghtOfEmploymentAcceptable = true
		}
		else{
			isIncomeAcceptable = false
			isLenghtOfEmploymentAcceptable = true
		}

		if(isIncomeAcceptable){
			loanApplication.isIncomeAmountEnough = true
		}
		else{
			loanApplication.isIncomeAmountEnough = false
		}

		if(isLenghtOfEmploymentAcceptable){
			loanApplication.isLengthOfEmployementEnough = true
		}
		else{
			loanApplication.isLengthOfEmployementEnough = false
		}
		if(isAgeAcceptable){
			loanApplication.isAgeEnough = true
		}
		else{
			loanApplication.isAgeEnough = false
		}


		logger.debug("isAgeAcceptable: " + isAgeAcceptable + ", isIncomeAcceptable: " + isIncomeAcceptable + ", isLenghtOfEmployment: " + isLenghtOfEmploymentAcceptable)

		if(loanApplication.isIncomeAmountEnough && loanApplication.isAgeEnough && loanApplication.isLengthOfEmployementEnough){
			loanApplication.isMinimumConditionApproved = true
			logger.debug("Minimumfeltétel megfelelő.")
		}
		else{
			loanApplication.isMinimumConditionApproved = false
			//			logger.debug("Minimumfeltétel nem megfelelő.")
			logger.debug("asd")
		}
		return loanApplication
	}
}